package org.juc.sync;

/**
 * 资源类
 *
 * @author Admin
 * @date 2021/11/22
 */
class Ticket {
    // 资源私有
    private int number = 30;
    // 资源类对外暴露的调用方法
    public synchronized void saleTicket(){
        if (number >0 ) {
            System.out.println(Thread.currentThread().getName()+"\t卖出第:"+number--+"张票"+"\t 还剩下："+number+"张票");
        }
    }
}
public class SaleTicket
{
    public static void main( String[] args )
    {
        final Ticket ticket = new Ticket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }
        },"A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }
        },"B").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }
        },"C").start();
    }

}

